package com.yf.zzmusic.service.impl;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.yf.zzmusic.common.Constants;
import com.yf.zzmusic.exception.ServiceException;
import com.yf.zzmusic.utils.AliyunOssUtils;
import com.yf.zzmusic.service.IUploadService;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class UploadService implements IUploadService{
  @Override
  public String  uploadImageFile(MultipartFile file) {
    AliyunOssUtils aliyunOssUtils =new AliyunOssUtils();
    EnvironmentVariableCredentialsProvider credentialsProvider=aliyunOssUtils.getCredentialsProvider();
    // 创建OSSClient实例。
//    OSS ossClient = new OSSClientBuilder().build(aliyunOssUtils.getEndpoint(), credentialsProvider);
    OSS ossClient = new OSSClient(aliyunOssUtils.getEndpoint(),aliyunOssUtils.getAccessKeyId(),aliyunOssUtils.getAccessKeySecret());
    String url = null;//返回的路径
    //获取上传文件流输入流
    InputStream inputStream;
    try {
      inputStream=file.getInputStream();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    //获取文件名称
    String fileName = "03_masu_gaduation/";
    //保证文件名唯一，去掉uuid中的'-'
    //file.getOriginalFilename()
    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy_MM_dd");
    String datePath=simpleDateFormat.format(new Date());
    fileName = fileName + datePath + "/";
    String uuid=UUID.randomUUID().toString().replaceAll("-","");
    fileName = fileName +  uuid + file.getOriginalFilename();
    // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt
    try {
      // 创建PutObjectRequest对象。
//      new PutObjectRequest(aliyunOssUtils.getBucketName(), fileName, inputStream);
      ossClient.putObject(aliyunOssUtils.getBucketName(), fileName, inputStream);
      url="https://" + aliyunOssUtils.getBucketName() + "." + aliyunOssUtils.getEndpoint() + "/" + fileName;
//      System.out.println(url);
    }catch (OSSException oe){
      System.out.println("Caught an OSSException, which means your request made it to OSS, "
          + "but was rejected with an error response for some reason.");
      System.out.println("Error Message:" + oe.getErrorMessage());
      System.out.println("Error Code:" + oe.getErrorCode());
      System.out.println("Request ID:" + oe.getRequestId());
      System.out.println("Host ID:" + oe.getHostId());
      throw new ServiceException(Constants.CODE_ERROR,oe.getErrorMessage());
    }catch (ClientException ce) {
      System.out.println("Caught an ClientException, which means the client encountered "
          + "a serious internal problem while trying to communicate with OSS, "
          + "such as not being able to access the network.");
      System.out.println("Error Message:" + ce.getMessage());
      throw new ServiceException(Constants.CODE_ERROR,ce.getMessage());
    }finally {
      if (ossClient != null) {
        ossClient.shutdown();
      }
    }
    return url;
  }
}
